﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using web.data.entities;
using web.bussines;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using web.core.Controllers;

namespace web.core.Models
{
    public class productModels
    {
        public static product GetProductByID(int productId) 
        {
            product_service proSvc = new product_service();
            return proSvc.GetProductByID(productId);
        } 

        public static void updateStatusProductCategory(product_category cate)
        {
            product_service proSvc = new product_service();
            proSvc.updateStatusProductCategory(cate);
        }
        
        public static int createProductCategory(product_category cate, List<product_category_item> items)
        {
            product_service proSvc = new product_service();
            return proSvc.createProductCategory(cate, items);
        }

        public static void deleteProductCategory(int category_id)
        {
            product_service proSvc = new product_service();
            proSvc.deleteProductCategory(category_id);
        }

        public static void deleteProductCategoryItem(int category_item_id)
        {
            product_service proSvc = new product_service();
            proSvc.deleteProductCategoryItem(category_item_id);
        }

        public static List<web.data.entities.product_category_item> GetListProductCategoryItemsByCateID(int category_id, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductCategoryItemsByCateID(category_id, languageId);
        }

        public static web.data.entities.product_category GetProductCategoryByID(int category_id)
        {
            product_service proSvc = new product_service();
            return proSvc.GetProductCategoryByID(category_id);
        }

        public static void editProductCategory(product_category cate, List<product_category_item> items)
        {
            product_service proSvc = new product_service();
            proSvc.editProductCategory(cate, items);
        }
         
        public static void updateStatusProduct(product pro)
        {
            product_service proSvc = new product_service();
            proSvc.updateStatusProduct(pro);
        }

        public static int createProduct(product pro, List<product_item> items, List<product_ref_category> list_ref,List<product_images> list_img, List<product_tag> lstTag)
        {
            product_service proSvc = new product_service();
            return proSvc.createProduct(pro, items, list_ref, list_img, lstTag);
        }

        public static List<web.data.entities.product_item> GetListProductItemsByProductID(int product_id, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductItemsByProductID(product_id, languageId);
        }

        public static List<int> GetListCategoryByProductID(int product_id)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListCategoryByProductID(product_id);
        }

        public static void DeleteAllProductRefByProductID(int product_id)
        {
            product_service proSvc = new product_service();
            proSvc.DeleteAllProductRefByProductID(product_id);
        }

        public static void editProduct(product pro, List<product_item> items, List<product_ref_category> list_ref, List<product_images> list_img, List<product_tag> lstTag)
        {
            product_service proSvc = new product_service();
            proSvc.editProduct(pro, items, list_ref, list_img, lstTag);
        }

        public static void deleteProduct(int product_id)
        {
            product_service proSvc = new product_service();
            //xoa ds anh sp
            var lstimg = proSvc.GetListProductImagesByProductID(product_id);
            if (lstimg != null) {
                foreach (var im in lstimg) {
                    deleteProductImages(im.images_id);
                }
            }
            //xoa anh sp 
            var item = proSvc.GetProductByID(product_id);
            if (item != null) {
                if (!string.IsNullOrEmpty(item.images))
                    baseController.DeleteFile(item.images);
            }
            //xoa db
            proSvc.deleteProduct(product_id);
        } 

        public static void deleteProductItem(int product_item_id)
        {
            product_service proSvc = new product_service();
            proSvc.deleteProductItem(product_item_id);
        }

        #region v_product

        public static v_product GetProductViewByID(int productId, string url)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(url);
            product_service proSvc = new product_service();
            return proSvc.GetProductViewByID(productId, languageId);
        }

        public static PagedList<v_product> GetListProductByCategoryID(int? languageId, int? cateId, int pageindex, int pageSize, string orderBy, string orderType, out int total)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductByCategoryID(languageId, cateId, pageindex, pageSize, orderBy, orderType, out total);
        }
        //danh sach san pham lien quan
        public static List<v_product> GetListProductRelated(int productId, int numberProduct)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductRelated(productId, numberProduct);
        }
        //lay danh sach nhung san pham ko lien quan 
        public static PagedList<v_product> GetListProductNotRelate(int productid, string text_search, int? languageId, int? cateId, int pageindex, int pagesite)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductNotRelate(productid, text_search, languageId, cateId, pageindex, pagesite);
        } 

        public static PagedList<v_product> GetListProductByCategoryIDAdm(string text_search, int? languageId, int? cateId, int pageindex, int pagesite, string orderBy, string orderType, out int total)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductByCategoryIDAdm(text_search, languageId, cateId, pageindex, pagesite, orderBy, orderType, out total);
        }

        public static PagedList<v_product> GetListProductSearch(int? languageId, string text_search, int pageindex, int pagesite, string orderBy, string orderType, out int total)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductSearch(languageId, text_search, pageindex, pagesite, orderBy, orderType, out total);
        }

        public static List<web.data.entities.v_product> GetListProductViewByParam(string category_param, int? languageId, int? number_items)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductViewByParam(category_param, languageId, number_items);
        }

        #endregion

        #region v_category_product

        public static List<v_category_product> getListProductCategoryByParent(int? id, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListCategoryProductByParent(id, languageId);
        }

        public static List<v_category_product> GetListCategoryProductByParentAdm(int? cateId, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListCategoryProductByParentAdm(cateId, languageId);
        }

        public static List<v_category_product> getProductCategoryDropDownlist(int? id, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.getProductCategoryDropDownlist(id, languageId);
        }

        public static List<v_category_product> getProductCategoryBuilder(string text_search, int? parent, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.getCategoryProductBuilder(text_search, parent, languageId);
        }


        public static web.data.entities.v_category_product GetProductCategoryViewByID(int category_id, int? languageId)
        {
            product_service proSvc = new product_service();
            return proSvc.GetProductCategoryViewByID(category_id, languageId);
        }
        #endregion

        #region product images
        public static List<product_images> GetListImagesByProduct(int productId, int numberImg)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListImagesByProduct(productId, numberImg);
        }

        public static List<web.data.entities.product_images> GetListProductImagesByProductID(int product_id)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductImagesByProductID(product_id);
        }

        public static int product_img_add(product_images it)
        {
            product_service proSvc = new product_service();
            return proSvc.product_img_add(it);
        }

        public static void deleteProductImages(int images_id)
        {
            product_service proSvc = new product_service();
            //delete image
            var item = proSvc.ProductImageGetById(images_id);
            if (item != null)
            {
                if (!string.IsNullOrEmpty(item.images))
                    baseController.DeleteFile(item.images);
                if (!string.IsNullOrEmpty(item.images_thumb))
                    baseController.DeleteFile(item.images_thumb);
            }
            //delete db
            proSvc.deleteProductImages(images_id);
        }
        public static product_images ProductImageGetById(int images_id){
            product_service proSvc = new product_service();
            return proSvc.ProductImageGetById(images_id);
        }
        #endregion
         
        #region product tag
        //get tag by product id
        public static List<product_tag> tag_getby_productid(int id)
        {
            product_service proSvc = new product_service();
            return proSvc.tag_getby_productid(id);
        }

        public static List<web.data.entities.product_tag> GetListProductTag()
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductTag();
        }

        public static PagedList<v_product> GetListProductByTag(int? languageId, string tag, int pageindex, int pagesite, string orderBy, string orderType, out int total)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductByTag(languageId, tag, pageindex, pagesite, orderBy, orderType, out total);
        }

        public static web.data.entities.product_tag GetProductTagById(int? tag_id)
        {
            product_service proSvc = new product_service();
            return proSvc.GetProductTagById(tag_id);
        }

        public static web.data.entities.product_tag GetTag(string tag)
        {
            product_service proSvc = new product_service();
            return proSvc.GetTag(tag);
        }

        public static List<web.data.entities.product_tag> GetListProductTagByProductID(int product_id)
        {
            product_service proSvc = new product_service();
            return proSvc.GetListProductTagByProductID(product_id);
        }
        #endregion

        #region product relate
        public static bool product_relate_add(product_relate item)
        {
            product_service proSvc = new product_service();
            return proSvc.product_relate_add(item);
        }

        public static bool product_relate_delete(int pid_a, int pid_b)
        {
           product_service proSvc = new product_service();
           return proSvc.product_relate_delete(pid_a, pid_b);
        }
        #endregion

        #region product relate
        public static List<v_product> ListOtherInCategory(int? languageId, int? cateId, int productId, int number) 
        {
            product_service proSvc = new product_service();
            return proSvc.ListOtherInCategory(languageId, cateId,productId,number);
        }
        #endregion
    }
}